package com.dean.ui.panel.impl;

import com.dean.constants.DeanUiConstants;
import com.dean.entity.SysDbConnInfo;
import com.dean.ui.DeanFormUIUtils;
import com.dean.ui.PageCallback;
import com.dean.ui.PageInfo;
import com.dean.ui.panel.DeanPanel;
import com.dean.ui.utils.UiUtils;
import com.dean.utils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @ClassName DbQueryPanel
 * @Description sql查询页面
 * @Author dean
 * @Date 2021/10/3 10:03
 */
public class DbQueryPanel2 extends BasePanel implements DeanPanel{

	private static final Logger logger = LoggerFactory.getLogger(DbQueryPanel2.class);
    /**
     * 配置 文件路径
     */
    private final static String INIT_PATH_NAME = DeanUiConstants.INIT_PATH + "dbquery.properties";

    private DeanFormUIUtils uiUtils;
    private JPanel panel;

    private JLabel usernameLabel;
    private JTextField usernameText;


	public void mainUi(){
        uiUtils = new DeanFormUIUtils();
        this.panel = uiUtils.buildPanel();
        this.usernameText = uiUtils.createText("用户名");
        uiUtils.createText("密码");
        uiUtils.createText("家庭地址",true);
        uiUtils.createText("籍贯");
        uiUtils.createTextArea("个人简介",true);
        uiUtils.createButton("查询");
        uiUtils.createButton("新增");
        uiUtils.createButton("修改");
        uiUtils.createButton("删除");
        uiUtils.createButton("明细");
        uiUtils.createButton("测试1");
        uiUtils.createButton("测试2");
        uiUtils.createButton("测试3");
        uiUtils.createButton("测试4");
        JTable table = uiUtils.createTable(new PageCallback() {
            @Override
            public PageInfo doPag(int pageSize, int pageNum) {
                PageInfo pageInfo = new PageInfo();
                pageInfo.setPageSize(pageSize);
                pageInfo.setPageNum(pageNum);
                pageInfo.setTotalPage(100);
                pageInfo.setTotal(10000);
                return pageInfo;
            }
        });
//        String[][] datas = new String[100][10];
//        for (int i = 0; i < 100; i++) {
//            String [] data = new String[10];
//            for (int j = 0; j < 10; j++) {
//                data[j]=("row:"+i+",col:"+j);
//            }
//            datas[i]=data;
//        }
////        DefaultTableModel tableModel = new DefaultTableModel(datas,datas[0]);
////        table.setModel(tableModel);
//        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//        table.setVisible(true);
//        table.setShowGrid(true);
//        // 第一行
//        JLabel label = new JLabel("用户名:");
//        constraints.gridx = 0;
//        constraints.gridy = 0;
//        constraints.gridwidth = 1;
//        constraints.gridheight = 1;
//        constraints.weightx = 0.05;
//        constraints.weighty = 0.01;
//        layout.setConstraints(label, constraints);
//        panel1.add(label);
////        panel1.add(label);
////        // label的上边里panel 的上边10
////        layout.putConstraint(DeanSpringLayout.TOP,label,10,DeanSpringLayout.TOP,panel1);
////        // label的左边离panel的左边10
////        layout.putConstraint(DeanSpringLayout.LEFT, label, 10,DeanSpringLayout.LEFT, panel1);
//
//        JTextField textField = new JTextField();
//        constraints.gridx = 1;
//        constraints.gridy = 0;
//        constraints.gridwidth = 1;
//        constraints.gridheight = 1;
//        constraints.weightx = 0.45;
//        constraints.weighty = 0.01;
//        layout.setConstraints(textField, constraints);
//        panel1.add(textField);
//        layout.putConstraint(SpringLayout.WEST, textField, 10, SpringLayout.EAST, label);
//        layout.putConstraint(SpringLayout.EAST, textField, -10,SpringLayout.EAST, panel1);
//        layout.putConstraint(SpringLayout.NORTH, textField, 10,SpringLayout.NORTH, panel1);

        // 第二行
//        JLabel passLabel = new JLabel("密码:");
//        constraints.gridx = 2;
//        constraints.gridy = 0;
//        constraints.gridwidth = 1;
//        constraints.gridheight = 1;
//        constraints.weightx = 0.05;
//        constraints.weighty = 0.01;
//        layout.setConstraints(passLabel, constraints);
//        panel1.add(passLabel);
//        // label的上边里panel 的上边10
//        layout.putConstraint(SpringLayout.NORTH,passLabel,10,SpringLayout.SOUTH,label);
//        // label的左边离panel的左边10
//        layout.putConstraint(SpringLayout.WEST, passLabel, 10,SpringLayout.WEST, panel1);

//        JTextField passwd = new JTextField();
//        constraints.gridx = 3;
//        constraints.gridy = 0;
//        constraints.gridwidth = 1;
//        constraints.gridheight = 1;
//        constraints.weightx = 0.45;
//        constraints.weighty = 0.01;
//        layout.setConstraints(passwd, constraints);
//        panel1.add(passwd);
//        layout.putConstraint(DeanSpringLayout.TOP, passwd, 10, DeanSpringLayout.BOTTOM, textField);
//        layout.putConstraint(DeanSpringLayout.LEFT, passwd, 10,DeanSpringLayout.RIGHT, passLabel);
//        layout.putConstraint(DeanSpringLayout.RIGHT, passwd, -10,SpringLayout.EAST, panel1);

//        JLabel passCodeLabel = new JLabel("验证码:");
//        panel1.add(passCodeLabel);
//        // label的上边里panel 的上边10
//        layout.putConstraint(DeanSpringLayout.TOP,passCodeLabel,10,DeanSpringLayout.BOTTOM,textField);
//        // label的左边离panel的左边10
//        layout.putConstraint(DeanSpringLayout.LEFT, passCodeLabel, 10,DeanSpringLayout.RIGHT, passwd);
////
//        JTextField passCode = new JTextField("xhdhdd");
//        panel1.add(passCode);
//        layout.putConstraint(DeanSpringLayout.TOP, passCode, 10, DeanSpringLayout.BOTTOM, textField);
//        layout.putConstraint(DeanSpringLayout.LEFT, passCode, 10,DeanSpringLayout.RIGHT, passCodeLabel);
//        layout.putConstraint(DeanSpringLayout.RIGHT, passCode, -10,DeanSpringLayout.RIGHT, panel1);
//        JLabel addrLabel = new JLabel("家庭地址");
//        constraints.gridx = 0;
//        constraints.gridy = 1;
//        constraints.gridwidth = 1;
//        constraints.gridheight = 1;
//        constraints.weightx = 0.05;
//        constraints.weighty = 0.01;
//        layout.setConstraints(addrLabel, constraints);
//        panel1.add(addrLabel);

//        JTextField addr = new JTextField("家庭地址");
//        constraints.gridx = 1;
//        constraints.gridy = 1;
//        constraints.gridwidth = 9;
//        constraints.gridheight = 1;
//        constraints.weightx = 0.95;
//        constraints.weighty = 0.01;
//        layout.setConstraints(addr, constraints);
//        panel1.add(addr);
//
//        JLabel descLabel = new JLabel("个人简介");
//        constraints.gridx = 0;
//        constraints.gridy = 2;
//        constraints.gridwidth = 1;
//        constraints.gridheight = 1;
//        constraints.weightx = 0.05;
//        constraints.weighty = 0.01;
//        layout.setConstraints(descLabel, constraints);
//        panel1.add(descLabel);
//
//        JScrollPane textAreaScroll = new JScrollPane();
//
//        JTextArea desc = new JTextArea();
//        desc.setRows(2);
//        constraints.gridx = 1;
//        constraints.gridy = 2;
//        constraints.gridwidth = 9;
//        constraints.gridheight = 1;
//        constraints.weightx = 0.95;
//        constraints.weighty = 0.01;
//        layout.setConstraints(textAreaScroll, constraints);
//        textAreaScroll.setViewportView(desc);
//        panel1.add(textAreaScroll);
//
//        JLabel blankrow = new JLabel("这是空行");
//        constraints.gridx = 0;
//        constraints.gridy = 3;
//        constraints.gridwidth = 4;
//        constraints.gridheight = 1;
//        constraints.weightx = 1;
//        constraints.weighty = 1-0.03;
//        layout.setConstraints(blankrow, constraints);
//        panel1.add(blankrow);
        this.uiUtils.finishCreate();
        scrollPane.setViewportView(this.panel);

//        connNameLable = new JLabel("connName:");
//        UiUtils.setLeftBounds(connNameLable,1);
//        panel.add(connNameLable);
//
//        connName = new JComboBox<String>();
//        UiUtils.setLeftBounds(connName,1);
//        panel.add(connName);
//
//        pageSizeLabel = new JLabel("pageSize:");
//        UiUtils.setRightBounds(pageSizeLabel,1);
//        panel.add(pageSizeLabel);
//
//        // 默认分页大小
//        pageSize = new JTextField("1000");
//        UiUtils.setRightBounds(pageSize,1);
//        panel.add(pageSize);
//
//        // sql输入窗口
//        sqlText = new JTextArea();
//        JScrollPane sqlInput = new JScrollPane(sqlText);
//        UiUtils.setTextAreaBounds(sqlText,2,4,1,2);
//        UiUtils.setTextAreaBounds(sqlInput,2,4,1,2);
//        panel.add(sqlInput);
//
//        tipLable = new JLabel("提示信息:");
//        UiUtils.setLeftBounds(tipLable,6);
//        panel.add(tipLable);
//
//        executeRes = new JTextField("");
//        UiUtils.setLeftBounds(executeRes,6,2);
//        executeRes.setForeground(Color.RED);
//        Font font = new Font("黑体", Font.PLAIN, 20);
//        executeRes.setFont(font);
//        executeRes.setEditable(false);
//        panel.add(executeRes);
//
//        // 执行按钮
//        // queryBtn = new JButton("查询");
//        queryBtn = ButtonBuilder.primaryButton("查询");
//        UiUtils.setRightButtonBounds(queryBtn,7,3);
//        panel.add(queryBtn);
//
//       // exportBtn = new JButton("导出查询结果");
//        exportBtn = ButtonBuilder.dangerButton("导出查询结果");
//        UiUtils.setRightButtonBounds(exportBtn,7,2);
//        panel.add(exportBtn);
//
//        saveCfgBtn = ButtonBuilder.greenButton("保存配置信息");
//        UiUtils.setRightButtonBounds(saveCfgBtn,7,1);
//        panel.add(saveCfgBtn);
//
//
//        // 查询列表窗口
//        table = new JTable();
//        tablelPane = new JScrollPane(table);
//        table.setShowGrid(true);
//        UiUtils.setTableBounds(tablelPane,8);
//        panel.add(tablelPane);
    }

    public void mainScript(){
//        initProperties();
//        exportBtn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                String [] fileTypes = {"EXCEL","CSV","INSERT SQL"};
//                JTextField filePath = new JTextField();
//                Object[] message = {
//                        "导出文件路径:", filePath,
//                        "导出文件类型:",
//                };
//                Object fileType = JOptionPane.showInputDialog(panel,message,"文件导出格式",1,null,fileTypes,null);
//                logger.debug("用户选择了导出:{}",fileType);
//                if(fileType == null|| "".equals(fileType)){
//                    JOptionPane.showMessageDialog(panel, "请选择文件的导出类型", "ERROR", JOptionPane.ERROR_MESSAGE);
//                    return;
//                }
//                if(filePath.getText() == null|| "".equals(filePath.getText())){
//                    JOptionPane.showMessageDialog(panel, "文件导出路径不能为空", "ERROR", JOptionPane.ERROR_MESSAGE);
//                    return;
//                }
//                if(dataQueryEntity == null|| dataQueryEntity.getCode() != 0){
//                    JOptionPane.showMessageDialog(panel, "表格数据为空,无法导出", "ERROR", JOptionPane.ERROR_MESSAGE);
//                    return;
//                }
//                DeanResponse<String> deanResponse =  DbQueryAction.getInstance().exportFile(dataQueryEntity,filePath.getText(),fileType.toString());
//                if(deanResponse.getCode() == 0){
//                    JOptionPane.showMessageDialog(panel, "FILE SAVE PATH IS :"+deanResponse.getData(), "SUCCESS", JOptionPane.INFORMATION_MESSAGE);
//                }else{
//                    JOptionPane.showMessageDialog(panel, deanResponse.getMsg(), "ERROR", JOptionPane.ERROR_MESSAGE);
//                }
//            }
//        });
//
//        queryBtn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                queryExecute();
//            }
//        });
//
//
//        // 下拉框处理
//        DbConnService.getInstance().getConnInfos().forEach(connInfo -> {
//            connName.addItem(connInfo.getConnName());
//        });
//
//
//        saveCfgBtn.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                if(saveProperties()) {
//                    JOptionPane.showMessageDialog(panel,"配置信息保存成功", "SUCESS",JOptionPane.INFORMATION_MESSAGE);
//                }else {
//                    JOptionPane.showMessageDialog(panel,"配置信息保存失败", "ERROR",JOptionPane.ERROR_MESSAGE);
//                }
//            }
//        });
//        // 监听键盘事件 只允许输入数字
//        pageSize.addKeyListener(new KeyListener() {
//            @Override
//            public void keyTyped(KeyEvent e) {
//                char key = e.getKeyChar();
//                Pattern pat = Pattern.compile("[0-9]");
//                if (!pat.matcher(String.valueOf(key)).matches()) {
//                    e.setKeyChar((char) KeyEvent.VK_CLEAR);
//                }
//            }
//
//            @Override
//            public void keyPressed(KeyEvent e) {
//
//            }
//
//            @Override
//            public void keyReleased(KeyEvent e) {
//
//            }
//        });
//
//    }
//
//	private void queryExecute(){
//        String querySql = sqlText.getText();
//        if(querySql == null|| "".equals(querySql)) {
//            JOptionPane.showMessageDialog(panel, "执行sql不能为空", "ERROR", JOptionPane.ERROR_MESSAGE);
//            return;
//        }
//        if(!querySql.trim().toLowerCase().startsWith(DeanUiConstants.START_SELECT_CHAR)) {
//            JOptionPane.showMessageDialog(panel, "非法sql，不允许执行", "ERROR", JOptionPane.ERROR_MESSAGE);
//            return;
//        }
//        if(querySql.trim().toLowerCase().contains(DeanUiConstants.SEMICOLON)){
//            JOptionPane.showMessageDialog(panel, "含有非法字符【;】", "ERROR", JOptionPane.ERROR_MESSAGE);
//            return;
//        }
//        if(Integer.parseInt(pageSize.getText())< 1 ){
//            JOptionPane.showMessageDialog(panel, "不支持的分页大小", "ERROR", JOptionPane.ERROR_MESSAGE);
//            return;
//        }
//        dataQueryEntity = DbQueryAction.getInstance().query(initDbConnect(),querySql,Integer.parseInt(pageSize.getText()));
//        if(dataQueryEntity.getCode() == 0){
//            DefaultTableModel tableModel = new DefaultTableModel(dataQueryEntity.getDatas(),dataQueryEntity.getHeader());
//            table.setModel(tableModel);
//        }
//        executeRes.setText(dataQueryEntity.getCode()+"==="+dataQueryEntity.getMsg());
//        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//        table.setVisible(true);
    }

    /**
     * 保存配置文件
     * @return
     */
	private boolean saveProperties() {
		 Properties properties = new Properties();
//         properties.setProperty("pageSize", pageSize.getText());
//         properties.setProperty("fieldInput", sqlText.getText());
         return PropertyUtils.saveProperties(INIT_PATH_NAME,properties);
	}

    /**
     * 初始化配置信息
     */
    @Override
    public void initProperties() {
        //初始化基础参数
//        Properties properties = PropertyUtils.readProperties(INIT_PATH_NAME);
//        if(properties != null){
//             pageSize.setText(properties.getProperty("pageSize"));
//             sqlText.setText(properties.getProperty("fieldInput"));
//        }
	}

    /**
     * 设置数据库连接配置
     */
    private SysDbConnInfo initDbConnect() {
        SysDbConnInfo connectInfo = new SysDbConnInfo();
//        connectInfo.setConnName(connName.getSelectedItem().toString());
        return connectInfo;
    }
}
